package top.tigerwoods.spring.base.core.unification;


import lombok.Data;
import lombok.experimental.Accessors;
import top.tigerwoods.spring.base.core.exception.IWoodsException;


import java.io.Serializable;

/**
 * description 系统统一返回对象
 * 系统或第三方应用产生的 exception code 代码应小于 0
 * woods 系统中主动抛出的 WoodException code 代码应大于 0
 */
@Data
@Accessors(chain = true)
public class WoodsResult implements Serializable {
    public static final String CODE_NAME = "";
    public static final String MESSAGE_NAME = "";
    public static final String CONTENT_NAME = "";

    private static final long serialVersionUID = 1699599378069244435L;
    /**
     * 返回代码 0：正常返回， 大于等于 1：有问题当前系统已处理(系统成功，业务不成功)，小于等于 -1：非当前系统产生的问题（系统不成功）
     */
    Integer code;
    /**
     * 返回的提示信息，
     */
    String message;
    /**
     * 返回的数据信息
     */
    Object contents;

    public WoodsResult(){

    }

    public WoodsResult(String message){
        this.code = 0;
        this.message = message;
    }

    public WoodsResult(Integer code, String message){
        this.code = code;
        this.message = message;
    }

    public WoodsResult(RuntimeException exception){
        this.code = exception instanceof IWoodsException ? ((IWoodsException) exception).getCode() : -1;
        this.message = null == exception.getMessage() ? "空值错误" : exception.getMessage();

    }

    public WoodsResult(Exception exception){
        this.code = exception instanceof IWoodsException ? ((IWoodsException) exception).getCode() : -1;
        this.message = null == exception.getMessage() ? "空值错误" : exception.getMessage();

    }

    public WoodsResult(Throwable throwable){
        this.code = -1;
        this.message = null == throwable.getMessage() ? "空值错误" : throwable.getMessage();
    }
}
